package com.tangsm.spring.boot.poi.tl.controller;

import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.config.Configure;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * 领导审核Controller
 *
 * @author tangyb
 * @date 2022-02-14
 */
@RestController
@RequestMapping("/poiTl")
public class ProcessControlController {

    private static final Logger log = LoggerFactory.getLogger(ProcessControlController.class);

    @GetMapping("/test")
    public void test(HttpServletResponse response) {
        XWPFTemplate template = null;

        try {

            String fileName = "D:\\output.docx";

            ClassPathResource filePath = new ClassPathResource("docs/processControl.docx");

            Map<String, Object> params = new HashMap<>();
            params.put("name", "中国");
            params.put("yes", false);

            //配置使用 spring SPEL表达式
            Configure configure = Configure.builder().useSpringEL().build();
            // 使用map构建数据
            template = XWPFTemplate.compile(filePath.getInputStream(), configure).render(params);

            // 输出流
            template.writeToFile(fileName);
            template.close();
        } catch (Exception e) {
            log.error("导出个人档案异常", e);
        } finally {
            if (template != null) {
                try {
                    template.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
        }
    }
}
